package com.stars.module.grant.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.yulichang.base.MPJBaseMapper;
import com.stars.module.grant.entity.StarGrant;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author : moon
 * @version : 1.0
 * @date : 2023/03/22
 * @description : 权限操作数据库映射
 */
@Mapper
public interface StarGrantMapper extends MPJBaseMapper<StarGrant> {


    @Delete("DELETE FROM STAR_GRANT WHERE ID=#{id}")
    boolean deleteGrantDataById(String id);

    @Delete("DELETE FROM STAR_GRANT WHERE SOURCE_ID=#{privilegeId} AND TARGET_TYPE=#{targetType}")
    void deleteGrantByType(String privilegeId, Integer targetType);

    @Select("SELECT * FROM STAR_GRANT G INNER JOIN STAR_GRANT G1 ON G.TARGET_ID = G1.SOURCE_ID " +
            " INNER JOIN STAR_ROLE R ON R.ID = G1.SOURCE_ID INNER JOIN STAR_PRIVILEGE P ON P.ID = G.SOURCE_ID" +
            " WHERE G1.TARGET_ID = #{userId} AND R.ROWSTATUS = 1 AND P.ROWSTATUS = 1")
    List<StarGrant> queryPrivilegeByRole (String userId);
}
